package com.ftg.learn.chapter09.pattern.builder;

/**
 * 菜单类
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/1/30 上午9:17
 */
public class Meal {


    private Item[] items;

    /**
     * 下标
     */
    private int index = 0;

    public Meal() {
        this.items = new Item[5];
    }

    /**
     * 追加一个商品
     *
     * @param item
     */
    public void addItem(Item item) {
        //点一个
        if (this.index >= 5) {
            throw new RuntimeException("先生/女士您好，你吃不了了");
        }
        this.items[this.index++] = item;
    }

    /**
     * 对商品的单价求和，统计出来当前订单的总钱数
     *
     * @return
     */
    public float getCost() {
        //总价钱
        float sum = 0;
        for (int i = 0; i < this.index; i++) {
            sum += this.items[i].price();
        }
        return sum;
    }

    /**
     * 打印小票
     */
    public void showItems() {
        System.out.println("商品名称 \t 商品单价 \t ");
        for (int i = 0; i < this.index; i++) {
            System.out.println(this.items[i].getName() + "\t\t" + this.items[i].price() + "\t");
        }
        System.out.println("小计:" + this.getCost());
    }
}
